package com.foreveross.dao.imp;

import java.util.List;

import org.springframework.stereotype.Component;

import com.foreveross.common.SQLUtil;
import com.foreveross.dao.NewsHouseDAO;
import com.foreveross.vo.NewsHouse;

@Component("newsHouseDAO")
public class NewsHouseDAOImp extends SQLUtil implements NewsHouseDAO {

	@Override
	public long findNewsHouseListCount(String newsId, String houseId)
			throws Exception {
		StringBuffer sqlStr = new StringBuffer();
		sqlStr.append(" select count(distinct a.id) from news_houses a ");
		sqlStr.append(" where 1=1 ");
		if(null!=houseId && !"".equals(houseId)){
			sqlStr.append(" and a.house_id ='" + houseId + "'");
		}
		if(null!=newsId && !"".equals(newsId)){
			sqlStr.append(" and a.news_id = '" + newsId + "'");
		}
		return Long.parseLong(findObejctBySQL(sqlStr.toString(),null).get(0).toString()) ;
	}
	
	@Override
	public List<NewsHouse> findNewsHouseList(String newsId,
			String houseId,int pageIndex,int pageSize) throws Exception {
		StringBuffer sqlStr = new StringBuffer();
		sqlStr.append(" select distinct a.* from news_houses a ");
		sqlStr.append(" where 1=1 ");
		
		if(null!=houseId && !"".equals(houseId)){
			sqlStr.append(" and a.house_id ='" + houseId + "'");
		}
		if(null!=newsId && !"".equals(newsId)){
			sqlStr.append(" and a.news_id = '" + newsId + "'");
		}
		return findObejctBySQL(sqlStr.toString(),NewsHouse.class, pageIndex,
				pageSize);
	}

	@Override
	public void deleteNewsHouse(String id) throws Exception {
		final String hql = "delete from NewsHouse t where t.id='" + id + "'";
		updateObejctByHql(hql);
	}

	@Override
	public void deleteNewsHouseByNewsId(String newsId) throws Exception {
		final String hql = "delete from NewsHouse t where t.hrNewsInfo.id='" + newsId + "'";
		updateObejctByHql(hql);
	}

}
